﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Collections;

namespace Show_LED_New
{
    class RemoteClient
    {
        //对于服务端获取到的 TcpClient 进行了一个包装 定义一个RemoteClient的类
        private TcpClient client;
        private NetworkStream streamToClient;
        private const int BufferSize = 8192;
        private byte[] buffer;

       


        public RemoteClient(TcpClient client)
        {
            // 打印连接到的客户端信息
            this.client = client;   //将其变量读进来
            Console.WriteLine("\nClient Connected!{0}<--{1}",client.Client.LocalEndPoint,client.Client.RemoteEndPoint);
            
            // 获得流
            streamToClient = client.GetStream();
            buffer=new byte[BufferSize];

            // 在构造函数中就开始准备读取
            AsyncCallback callBack = new AsyncCallback(ReadComplete);
            streamToClient.BeginRead(buffer, 0, BufferSize, callBack, null);
            
        }

        //发送数据

        public void SendDown()
        {
            // 发送叉子下降指令
            byte[] cmd = new byte[3];
            cmd[0] = 0x15;
            cmd[1] = 0xa5;
            cmd[2] = 0x5a;
            try
            {
                streamToClient.Write(cmd,0,3);
            }
            catch
            {
                lock (streamToClient)
                {
                    AsyncCallback callback = new AsyncCallback(ReadComplete);
                    streamToClient.BeginRead(buffer, 0, BufferSize, callback, null);
                }
            }
        }

        public void Send_Up()
        {
            // 发送叉子下降指令
            byte[] cmd = new byte[3];
            cmd[0] = 0x16;
            cmd[1] = 0xa5;
            cmd[2] = 0x5a;
            try
            {
                streamToClient.Write(cmd, 0, 3);
            }
            catch
            {
                lock (streamToClient)
                {
                    AsyncCallback callback = new AsyncCallback(ReadComplete);
                    streamToClient.BeginRead(buffer, 0, BufferSize, callback, null);
                }
            }
        }
        public void SendMessage(string msg)
        {
         //   msg = string.Format("[length={0}]{1}", msg.Length, msg);

            byte[] temp = Encoding.ASCII.GetBytes(msg);
              
                try
                {
                    streamToClient.Write(temp, 0, temp.Length);
                    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            

            lock (streamToClient)
            {
                AsyncCallback callback = new AsyncCallback(ReadComplete);
                streamToClient.BeginRead(buffer, 0, BufferSize, callback, null);
            }
        }

        // 在读取完成时进行回调
        private void ReadComplete(IAsyncResult ar)
        {
            int bytesRead = 0;
            try
            {
                lock(streamToClient)
                {
                    bytesRead = streamToClient.EndRead(ar);
                    Console.WriteLine("Reading data {0} bytes",bytesRead);
                }
                if (bytesRead == 0) throw new Exception("读取到0字节");
                // 在这里作命令的解析
                if(bytesRead>=2)
                {
                    if (buffer[0] == 1 && buffer[1] == 1) MainView.Make_cmd("上");
                    if (buffer[0] == 1 && buffer[1] == 3) MainView.Make_cmd("下");
                    if (buffer[0] == 1 && buffer[1] == 2) MainView.Make_cmd("左");
                    if (buffer[0] == 1 && buffer[1] == 4) MainView.Make_cmd("右");
                    if (buffer[0] == 4 && buffer[1] == 5) MainView.Make_cmd("确定");
                    if (buffer[0] == 4 && buffer[1] == 4) MainView.Make_cmd("返回");
                    if (buffer[0] == 2 && buffer[1] == 1) MainView.Make_cmd("放大");
                    if (buffer[0] == 3 && buffer[1] == 3) MainView.Make_cmd("复位");
                    if (buffer[0] == 2 && buffer[1] == 2) MainView.Make_cmd("缩小");
                }
                
                string msg = Encoding.Unicode.GetString(buffer, 0, bytesRead);
                msg = Encoding.ASCII.GetString(buffer,0,bytesRead);
                lock (streamToClient)
                {
                    AsyncCallback callback = new AsyncCallback(ReadComplete);
                    streamToClient.BeginRead(buffer, 0, BufferSize, callback, null);
                }
                
            }
            catch(Exception ex)
            {
                    lock (streamToClient)
                    {
                        AsyncCallback callback = new AsyncCallback(ReadComplete);
                        streamToClient.BeginRead(buffer, 0, BufferSize, callback, null);
                    }
            }
        }
    }
}
